package x.zero.gen;

import org.beetl.core.Template;
import org.beetl.sql.gen.BaseProject;
import org.beetl.sql.gen.Entity;
import org.beetl.sql.gen.SourceConfig;
import org.beetl.sql.gen.simple.BaseTemplateSourceBuilder;

import java.io.Writer;
import java.util.Arrays;

public class ZeroMapperSourceBuilder extends BaseTemplateSourceBuilder {

    /**
     * 指定模板的路径
     */
    public static  String mapperPath = "zero_mapper.btl";


    public ZeroMapperSourceBuilder() {
        super("mapper");
    }

    public ZeroMapperSourceBuilder(String name) {
        super(name);
    }

    //Override
    @Override
    public void generate(BaseProject project, SourceConfig config, Entity entity) {


        Template template = groupTemplate.getTemplate(mapperPath);
        String mapperClass = entity.getName() + "Mapper";
        template.binding("className", mapperClass);
        template.binding("package", project.getBasePackage(this.name));
        template.binding("entityClass", entity.getName());
        //得到生成的entity的包
        String entityPkg = project.getBasePackage("pojo.entity");
        String mapperHead =  entityPkg + ".*" ;
        template.binding("imports", Arrays.asList(mapperHead));
        Writer writer = project.getWriterByName(this.name,entity.getName()+"Mapper.java");
        template.renderTo(writer);



    }
}
